<template>
  <homePannel ref="target" title="人气推荐" sub-title="人气爆款 不容错过">
    <!-- 作用域插槽 -->
    <template #right>
      <!-- 发现更多链接组件 -->
      <XtxMore path="/"></XtxMore>
    </template>
    <ul class="goodsList" v-if="RecommandGoodsList.length>0">
      <li v-for="item in RecommandGoodsList" :key="item.id">
        <RouterLink :to="`/goods/${item.id}`">
          <img v-imgLazy="item.picture" alt="">
          <p class="name ellipsis">{{item.alt}}</p>
          <p class="desc">{{item.title}}</p>
        </RouterLink>
      </li>
    </ul>
    <template v-else>
      <ul class="goodsList" v-if="RecommandGoodsList.length>0">
      <li v-for="i in 4" :key="i">
         <XtxSkeleton :width="300" :height="406" bg="rgba(255,255,255,.2)" style="margin-right:5px"></XtxSkeleton>
         <XtxSkeleton :width="300" :height="406" bg="rgba(255,255,255,.2)" style="margin-right:5px"></XtxSkeleton>
          <XtxSkeleton :width="300" :height="406" bg="rgba(255,255,255,.2)" style="margin-right:5px"></XtxSkeleton>
      </li>
    </ul>
    </template>
  </homePannel>
</template>

<script>
import { ref } from 'vue'
import homePannel from './homePannel.vue'
import { RecommandGoods } from '@/api/home/index.js'
// 导入监视器
import { useIntersectionObserver } from '@vueuse/core'
export default {
  name: 'RecommandGoods',
  components: {
    homePannel
  },
  setup () {
    // 初始化监视对象
    const target = ref(null)
    // 初始化数组存储商品数组
    const RecommandGoodsList = ref([])
    const { stop } = useIntersectionObserver(target, ([{ isIntersecting }], observerElement) => {
      if (isIntersecting) {
        // 如果进入了监视区域，执行请求数据逻辑
        // 停止监视
        stop()
        RecommandGoods().then(data => {
          // 给响应式数据数组赋值商品信息
          console.log(data, '这是人气推荐数据')
          RecommandGoodsList.value = data.result
        })
      }
    },
    { threshold: 0.5 })
    // RecommandGoods().then((data) => {
    //   // 给响应式数据数组赋值商品信息
    //   console.log(data, '这是人气推荐数据')
    //   RecommandGoodsList.value = data.result
    // })
    return {
      RecommandGoodsList, target
    }
  }
}
</script>

<style lang="less" scoped>
.goodsList {
  display: flex;
  justify-content: space-between;
  height: 406px;
  li {
    width: 300px;
    height: 406px;
    background-color: #f0f9f4;
    .hoverShadow();
    img {
      width: 306px;
      height: 306px;
    }
    p {
      font-size: 22px;
      padding: 12px 30px 0px;
      text-align: center;
    }
    .desc {
      padding: 12px 0px 0;
      color: #999999;
    }
  }
}
</style>
